package com.gxa.jjty.admin.service.impl;

import com.gxa.jjty.admin.service.ScoreService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.AppServe;
import com.gxa.jjty.core.pojo.Score;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

@Service("scoreManagementService")
@Transactional(rollbackFor = Throwable.class)
public class ScoreServiceImpl extends BaseServiceImpl<Score> implements ScoreService {


    @Autowired
    Mapper<Score>  scoreMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    ListOperations<String,Object> listOps = null;


    public static final String HEART_RATE="heartRateList";
    public static final String BLOOD_GLUCOSE="bloodGlucoseList";
    public static final String BLOOD_OXYGEN="bloodOxygenList";
    public static final String BLOOD_FAT="bloodFatList";
    public static final String BLOOD_PRESSURE="bloodPressureList";
    public static final String FOOD="foodList";

    public String   getKey(String name){
        String key = null;
        if ("心率".equals(name)){
            key = HEART_RATE;
        }
        if ("血糖".equals(name)){
            key = BLOOD_GLUCOSE;
        }
        if ("血氧".equals(name)){
            key = BLOOD_OXYGEN;
        }
        if ("血脂".equals(name)){
            key = BLOOD_FAT;
        }
        if ("膳食".equals(name)){
            key = FOOD;
        }
        if ("血压".equals(name)){
            key = BLOOD_PRESSURE;
        }
        return  key;
    }

    @Override
    public Score selectById(Long id) throws BusinessException {

        Score score = scoreMapper.selectByPrimaryKey(id);
        if (score == null){
            throw new BusinessException(ExceptionCode.SCORE_NO_EXIST);
        }
        return score;
    }

    @Override
    public void updateScore(Score score) throws BusinessException {
        listOps = redisTemplate.opsForList();
        scoreMapper.selectByPrimaryKey(score.getId());
        String name = score.getName();
        String key = getKey(name);
        listOps.remove(key,1,score);
        int i = scoreMapper.updateByPrimaryKeySelective(score);
        if (i != 1){
            throw  new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
        listOps.rightPush(key,score);
        System.out.println(i);
    }

    @Override
    public void deleteById(Long id) throws BusinessException {
        listOps = redisTemplate.opsForList();
        Score score = scoreMapper.selectByPrimaryKey(id);
        String name = score.getName();
        String key = getKey(name);
        listOps.remove(key,1,score);
        int i = scoreMapper.deleteByPrimaryKey(id);
        if (i != 1){
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }

    }

    @Override
    public void insertScore(Score score) throws BusinessException {
        listOps = redisTemplate.opsForList();
        int i = scoreMapper.insertSelective(score);
        if(i != 1){
            throw  new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
        String name = score.getName();
        String key = getKey(name);
        listOps.rightPush(key,score);
    }

    @Override
    public  void deleteAppServe(Long id) throws BusinessException {
        Score score = scoreMapper.selectByPrimaryKey(id);
        String name = score.getName();
        listOps.remove(getKey(name),1,score);
        int i = scoreMapper.deleteByPrimaryKey(id);
        if (i != 1){
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }


}
